{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db10eedc",
   "metadata": {},
   "source": [
    "# recap\n",
    "\n",
    "$\n",
    "y = XW + b\n",
    "$\n",
    "\n",
    "$\n",
    "y = \\Sigma{x_{i} * w_{i} + b}\n",
    "$\n",
    "\n",
    "#### 单层感知机\n",
    "\n",
    "$\n",
    "E = \\frac{1}{2}(O^{1}_{0} - t)^{2}\n",
    "$\n",
    "\n",
    "$\n",
    "\\frac{\\delta{E}}{\\delta{w_{j0}}} = ()\n",
    "$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "db8dd0ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.nn import functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "968153e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.6106,  0.3950, -0.6881, -0.1709, -1.7297,  0.7065, -0.6627, -1.3302,\n",
      "          1.4026,  0.5559]])\n",
      "tensor([[-0.4874, -0.4956,  0.7409,  0.6060,  1.1516,  0.4072, -1.9984, -0.2743,\n",
      "          0.6001, -0.4284]], requires_grad=True)\n",
      "torch.Size([1, 1])\n",
      "torch.Size([])\n",
      "tensor([[ 0.1466, -0.0948,  0.1651,  0.0410,  0.4152, -0.1696,  0.1591,  0.3193,\n",
      "         -0.3366, -0.1334]])\n"
     ]
    }
   ],
   "source": [
    "# pytorch 计算单层感知机\n",
    "\n",
    "# 输入的x有10个特征\n",
    "x = torch.randn(1, 10)\n",
    "w = torch.randn(1, 10, requires_grad=True)\n",
    "print(x)\n",
    "print(w)\n",
    "\n",
    "o = torch.sigmoid(x@w.t())\n",
    "print(o.shape)\n",
    "\n",
    "loss = F.mse_loss(torch.ones(1, 1), o)\n",
    "print(loss.shape)\n",
    "\n",
    "loss.backward()\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfcac010",
   "metadata": {},
   "source": [
    "# Perceptron 多层感知机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f2ea17ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 2])\n",
      "tensor(0.5631, grad_fn=<MseLossBackward0>)\n",
      "tensor([[ 0.1225, -0.1461,  0.0823,  0.1350, -0.1053,  0.2478, -0.0803,  0.0649,\n",
      "          0.1367, -0.0955],\n",
      "        [ 0.0696, -0.0830,  0.0468,  0.0767, -0.0598,  0.1408, -0.0456,  0.0369,\n",
      "          0.0777, -0.0543]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(1, 10)\n",
    "w = torch.randn(2, 10, requires_grad=True)\n",
    "\n",
    "o = torch.sigmoid(x@w.t())\n",
    "print(o.shape)\n",
    "\n",
    "loss = F.mse_loss(torch.ones(1, 2), o)\n",
    "print(loss)\n",
    "\n",
    "loss.backward()\n",
    "\n",
    "# [2, 10]\n",
    "print(w.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6274ef92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
